﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
using System.Data;

namespace TCMSWeb
{
    public partial class ReportToFileControl : System.Web.UI.UserControl
    {

        public string HeaderTxt;

        private string column;
        public string Column
        {
            get { return column; }
            set { column = value; }
        }
        private DataSet mainReportDataSet;
        public DataSet MainReportDataSet
        {
            set { mainReportDataSet = value; }
        }
      
        private List<DataSet> subreportDataSets;
        public List<DataSet> SubreportDataSets
        {
            set { subreportDataSets = value; }
        }
        public string MainReportDataSetName;
        public string ReportName;
        public string PDFFileName;
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.Visible = false;
        }
        private string renderType;
        public string RenderType
        {
            get { return renderType; }
            set
            {
                renderType = value;
            }
        }
       
        //public string RenderTo(string render)
        //{
          
        //    switch (render)
        //    {
        //        case "Image":
        //            {
        //                render = "Image";
        //                return render;
                        
        //            }
        //        case "Excel":
        //            {
        //                render = "Excel";
        //                return render;
                        
        //            }
        //        case "PDF":
        //            {
        //                render = "PDF";
        //                return render;
                     
        //            }
        //        default:
        //            {
        //                render = "PDF";
        //                return render;
                       
        //            }
        //    }
        //}
          

        public void RenderReport()
        {
            //string sql = "select * from ViewEnterpriseProgressReports";
            //DataSet ds = SqlHelper.ExecuteDataset(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql,
            //     new SqlParameter("EnterpriseID", 0));
            //ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports\\EnterpriseProgressReportList.rdlc");
            //ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("ViewEnterpriseProgressReports_ViewEnterpriseProgressReports", ds.Tables[0]));
            ReportViewer1.LocalReport.EnableHyperlinks = true;
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports\\" + ReportName);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource(MainReportDataSetName, mainReportDataSet.Tables[0]));
            ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { new ReportParameter("HeaderTxt", HeaderTxt),new ReportParameter("Columns",column) });
            //Selecting Rendering Type
            RenderType = ddlRenderType.SelectedValue.ToString();
            // Add a handler for SubreportProcessing.  
            ReportViewer1.LocalReport.SubreportProcessing +=
                        new SubreportProcessingEventHandler(DemoSubreportProcessingEventHandler);

            Warning[] warnings;

            string[] streamids;
            string mimeType;
            string encoding;
            string extension = "";
            //try
            //{
            byte[] bytes = ReportViewer1.LocalReport.Render(renderType, null, out mimeType, out encoding, out
                extension, out streamids, out warnings);
           
            Response.Clear();
            Response.ContentType = "application/"+renderType;
            Response.AddHeader("Content-disposition", "filename=" + PDFFileName + "."+extension);
            Response.OutputStream.Write(bytes, 0, bytes.Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
            Response.Flush();
            Response.Close();
          //   this.if1.RenderControl(
            //}

            //catch (Exception ex)
            //{
            //}
        }

        void DemoSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
        {
            //if (subreportDataSets)
            //if (orderDetailsData == null)
            //    orderDetailsData = LoadOrderDetailsData();
            //e.DataSources.Add(new ReportDataSource("DataSet1_OrderDetails", orderDetailsData));
            foreach (string dsName in e.DataSourceNames)
            {
                DataSet ds = subreportDataSets.Find(delegate(DataSet d) { return d.DataSetName == dsName; });
                e.DataSources.Add(new ReportDataSource(dsName, ds));
            }

        }

      

    }
}